package com.woniuxy.handler;

import java.util.Random;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.LockedAccountException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.woniuxy.pojo.User;
import com.woniuxy.service.UserService;

import lombok.Data;

@Data
@Controller
@RequestMapping("/user")

public class UserHandler {
	@Resource
	private UserService userService;
	
	@RequestMapping("/login")
	@ResponseBody
	public String login(HttpServletRequest req,String account,String pass,Boolean rm){
		User user = new User();
		user.setAccount(account);
		user.setPass(pass);
		//获取subject对象
		Subject currentUser = SecurityUtils.getSubject();
		//判断当前用户是否已经认证过了
		if(!currentUser.isAuthenticated()){
			//进行认证
			UsernamePasswordToken token = new UsernamePasswordToken(user.getAccount(), user.getPass());
			System.out.println(token);
			//remember me
			if(rm != null){
				token.setRememberMe(true);
			}
			
			try{
				currentUser.login(token);//进行认证
				System.out.println("认证成功！");
				//存uid
				int uid = userService.findUserByAccount(account).getUid();
				HttpSession hs = req.getSession();
				hs.setAttribute("uid", uid);
				String role = userService.findUserByAccount(account).getUstate();
			    if(role.equals("SuperAdmin")){
			     return "SuperAdmin";
			    }else if(role.equals("normal")){
			     return "登录成功！";
			    }else if(role.equals("FrontAdmin")){
			     return "FrontAdmin";
			    }else if(role.equals("SystemAdmin")){
			     return "SystemAdmin";
			    }
			}catch(UnknownAccountException e){
				System.out.println("失败失败");
				return "登录失败！";
			}
		}
		return "登录失败！";	
	}
	
	@RequestMapping("/tellogin")
	@ResponseBody
	public void tellogin(HttpServletRequest req,String tel){
		System.out.println("进");
		User user = userService.findUserByTel(tel);
		int uid = user.getUid();
		HttpSession hs = req.getSession();
		hs.setAttribute("uid", uid);
		
		//获取subject对象
		Subject currentUser = SecurityUtils.getSubject();
		//判断当前用户是否已经认证过了
		if(!currentUser.isAuthenticated()){
			System.out.println("认证");
			//进行认证
			UsernamePasswordToken token = new UsernamePasswordToken(user.getAccount(), user.getPass());
		try{
			currentUser.login(token);//进行认证
			}catch(UnknownAccountException e){
				System.out.println("失败失败");
			}
		}
	}
	
	
	@RequestMapping("/register")
	@ResponseBody
	public String register(String account,String pass,String tel,String mname){
		String result="注册失败";
		int a = (int) (Math.floor(Math.random()*(999999-100000))+100000);
		String mnumber = a+"";
		String ustate="normal";
		String mtype="普通会员";
		//注册会员
		int i = userService.regmember(mnumber,mname,tel,mtype);
		if(i>0){
			//获取mid
			int mid = userService.findMidByMnumber(mnumber);
			//注册用户
			int j = userService.register(account,pass,mid, ustate);
			if(j>0){
				result="注册成功！会员卡号："+mnumber;
			}
		}
		return result;
	}
	//通过用户id 查询用户详细信息
		@RequestMapping("/xqing")
		@ResponseBody
		public User FindIdByUser(HttpServletRequest request){
			User user=new User();
			HttpSession session = request.getSession();
			if(session!=null){
				 Integer uid = (Integer)session.getAttribute("uid");
				 user = userService.FindIdByUser(uid);
				 System.out.println("查出的信息"+user);
				 System.out.println("---------"+uid);
			}else {
				System.out.println("----------请先登录-------------");
				user=null;
			}
			return user;
		}
		
		//通过用户id 查询用户身份
		@RequestMapping("/sf")
		@ResponseBody
		public String findSf(HttpServletRequest request){
			String sf="";
			String ss="";
			HttpSession session = request.getSession();
			if(session!=null){
				 Integer uid = (Integer)session.getAttribute("uid");
				 ss = userService.Findsf(uid);
				 if(ss.equals("normal")){
					 sf="个人";
				 }else if(ss.equals("group")){
					 sf="团体";
				 }
				 System.out.println("查出的身份"+sf);
			}else {
				System.out.println("----------请先登录-------------");
			}
			System.out.println("sf-------"+sf);
			return sf;
		}	
	
		//使用ModelAndView方式取值
//		@RequestMapping("/xqing")
//		@ResponseBody
//		public ModelAndView FindIdByUser(HttpServletRequest request){
//			System.out.println("--------------------------------");
//			ModelAndView modelAndView=new ModelAndView();
//			HttpSession session = request.getSession();
//			Integer uid = (Integer)session.getAttribute("uid");
//			System.out.println("uid"+uid);
//			if(uid!=0&&uid!=null){
//				System.out.println("22222");
//				User user = userService.FindIdByUser(uid);
//				modelAndView.addObject("user", user);
//			}else {
//				modelAndView.setViewName("/index.html");
//			}
//			return modelAndView;
}
